package com.jn.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.jn.entity.code.Code;
import com.jn.service.code.CodeService;

@Controller
public class CodeUtil {
	@Autowired
	protected CodeService codeService;
	static CodeUtil instance;

	public CodeUtil() {
		instance = this;
	}

	/**
	 * 查询编码名称
	 * 
	 * @param type
	 * @param value
	 * @return
	 */
	public static String getName(String type, String value) {
		return instance.codeService.findNameByValue(type, value);
	}

	/**
	 * 查询编码值
	 * 
	 * @param type
	 * @param name
	 * @return
	 */
	public static String getValue(String type, String name) {
		return instance.codeService.findValueByName(type, name);
	}

	/**
	 * 查询编码列表
	 * 
	 * @param type
	 * @return
	 */
	public static Map<String, String> getCodeMap(String type) {
		List<Code> ls = instance.codeService.queryByType(type);
		Map<String, String> map = new LinkedHashMap<String, String>();
		for (Code c : ls) {
			map.put(c.getValue(), c.getName());
		}
		return map;
	}

}
